package com.situ.day18;

import static org.junit.Assert.assertEquals;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.junit.Test;



public class JDBCDemo {
	@Test
	public void test1() {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		//1、加载驱动Class.forName("");
		try {
			connection = JDBCUtil.getConnection();
			//3、写sql语句
			String sql = "SELECT id,`name`,age,gender FROM student;";
			//4、创建Statement
			statement = connection.createStatement();
			//5、执行sql语句
			//     (1) 更新类（更改了表里面数据）：delete/update/insert     executeUpdate()
			//           返回值：int，表示你影响的行数
			//      (2)查询（没有改变表里面数据）:  select                              executeQuery()
			//           返回值：结果集ResultSet
			resultSet = statement.executeQuery(sql);
			ArrayList<Student> list = new ArrayList<>();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			//6、关闭连接
			JDBCUtil.close(connection, statement, resultSet);
		}
	}
	
	@Test
	public void testUpdate() {
		Connection connection = null;
		Statement statement = null;
		//1、加载驱动Class.forName("");
		try {
			connection = JDBCUtil.getConnection();
			//3、写sql语句
			String sql = "delete from student where name='王五';";
			//4、创建Statement
			statement = connection.createStatement();
			//5、执行sql语句
			//     (1) 更新类（更改了表里面数据）：delete/update/insert     executeUpdate()
			//           返回值：int，表示你影响的行数
			//      (2)查询（没有改变表里面数据）:  select                              executeQuery()
			//           返回值：结果集ResultSet
			int count = statement.executeUpdate(sql);
			System.out.println("count:" + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			//6、关闭连接
			JDBCUtil.close(connection, statement, null);
		}
	}
	
	
	
	
}
